using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace DeclarativeTest.Core.Support
{
	public static class DictionaryExtensions
	{
		public static TValue GetValue<TKey,TValue>( this IDictionary<TKey,TValue> dictionary, TKey key, TValue defaultValue )
		{
			TValue value;
			if( !dictionary.TryGetValue( key, out value ) )
			{
				value = defaultValue;
			}
			return value;
		}

		public static TResult GetValue<TKey,TValue,TResult>( this IDictionary<TKey,TValue> dictionary, TKey key, TResult defaultValue )
		{
			TResult result;
			TValue value;
			if( dictionary.TryGetValue( key, out value ) )
			{
				result = (TResult) TypeDescriptor.GetConverter( typeof ( TResult ) ).ConvertFrom( value );
			}
			else
			{
				result = defaultValue;
			}
			return result;
		}

		public static IDictionary AsDictionary<TKey,TValue>( this IDictionary<TKey, TValue> dictionary )
		{
			IDictionary result = new Hashtable( StringComparer.InvariantCultureIgnoreCase );
			foreach ( KeyValuePair<TKey, TValue> pair in dictionary )
			{
				result.Add( pair.Key, pair.Value );
			}
			return result;
		}



	}
}